/*
 * Egress_Router.cc
 *
 *  Created on: May 20, 2011
 *      Author: ubuntu
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <omnetpp.h>

#include "mpls_qos.h"
#include "MPLSPacket.h"

#include "RAR_m.h"
#include "RAA_m.h"
#include "LSP_Cost_Update_m.h"

class Ergress_Router : public cSimpleModule
{
	//LSP figure 2
    LSP *newLSP1,*newLSP2,*newLSP3,*newLSP4,*newLSP5,*newLSP6,*newLSP7,*newLSP8,*newLSP9;

    protected:
	virtual void 			  initialize();
	virtual RAA              *generateNewMessage(int Ingress_Router_Index, int Requested_Bandwidth, bool Reserve  );
	virtual LSP_Cost_Update  *generateNewMessage(int LSP_ID, int cost);
	virtual void              handleMessage(cMessage *msg);
	virtual void              handleMessage(MPLSPacket *msg);
	virtual void              handleMessage(RAR *msg);



};

Define_Module(Ergress_Router);

void Ergress_Router::initialize()
{
				   newLSP1= new LSP(5,1,0,"IR1");
				   newLSP1->AddChain("CR4",4,0)->AddChain("CR8",8,0)->AddChain("CR12",12,0)->AddChain("ER",14,-1);
				   newLSP2= new LSP(5,1,0,"IR1");
				   newLSP2->AddChain("CR5",5,0)->AddChain("CR9",9,0)->AddChain("ER",14,-1);
				   newLSP3= new LSP(5,1,0,"IR1");
				   newLSP3->AddChain("CR5",4,0)->AddChain("CR8",8,0)->AddChain("CR12",12,0)->AddChain("ER",14,-1);
				   newLSP4= new LSP(5,2,0,"IR2");
				   newLSP4->AddChain("CR5",5,0)->AddChain("CR9",9,0)->AddChain("ER",14,-1);
				   newLSP5= new LSP(5,2,0,"IR2");
				   newLSP5->AddChain("CR6",6,0)->AddChain("CR9",9,0)->AddChain("ER",14,-1);
				   newLSP6= new LSP(5,2,0,"IR2");
				   newLSP6->AddChain("CR6",6,0)->AddChain("CR10",10,0)->AddChain("ER",14,-1);
				   newLSP7= new LSP(5,3,0,"IR3");
				   newLSP7->AddChain("CR7",7,0)->AddChain("CR11",11,0)->AddChain("CR13",13,0)->AddChain("ER",14,-1);
				   newLSP8= new LSP(5,3,0,"IR3");
				   newLSP8->AddChain("CR6",6,0)->AddChain("CR10",10,0)->AddChain("ER",14,-1);
				   newLSP9= new LSP(5,3,0,"IR3");
				   newLSP9->AddChain("CR7",7,0)->AddChain("CR10",10,0)->AddChain("CR13",13,0)->AddChain("ER",14,-1);
}

void Ergress_Router::handleMessage(MPLSPacket *msg) {}
void Ergress_Router::handleMessage(RAR *msg) {}

void Ergress_Router::handleMessage(cMessage *msg)
{
    if (uniform(0,1) < 0.1)
    {
        EV << "\"Losing\" message " << msg << endl;
        bubble("message lost");
        delete msg;
    }
    else
    {
        EV << msg << " received, sending back an acknowledgement.\n";
        delete msg;
       // send(new cMessage("ack"), "gate");
    }
}

RAA *Ergress_Router::generateNewMessage(int Ingress_Router_Index, int Requested_Bandwidth, bool Reserve)
{
	RAA *msg = new RAA();

	msg->setReserve(Reserve);
	msg->setIngress_Router_Index(Ingress_Router_Index);
	msg->setRequested_Bandwidth(Requested_Bandwidth);

	return msg;
}

LSP_Cost_Update  *Ergress_Router::generateNewMessage(int LSP_ID, int cost)
{
	LSP_Cost_Update *msg = new LSP_Cost_Update();

	return msg;
}

